package com.person.jc.service.buy;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.person.jc.service.Service;
import com.person.jc.utils.CreateCode;
import com.person.jc.utils.Utils;

public class BsBuyApplyService extends Service {
	
	public final static BsBuyApplyService me = new BsBuyApplyService();
	
	public List<Record> getList(Map<String, Object> map){
		String temp = "";
		List<Object> params = new ArrayList<Object>();
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT t.*,t1.kind_name use_to_name,t2.kind_name status_name,(SELECT SUM(unit_price*num) FROM bs_buy_product WHERE buy_id = t.buy_id) total_price,t3.user_name buy_user_name FROM bs_buy_apply t ");
		sql.append("LEFT JOIN ts_code_kind_data t1 ON t.buy_use = t1.kind_code AND t1.type_code='use_to' ");
		sql.append("LEFT JOIN ts_code_kind_data t2 ON t.status = t2.kind_code AND t2.type_code='buy_status'  ");
		sql.append("left join ts_user t3 on t.buy_user = t3.user_id ");
		sql.append(" where buy_user = ? ");
		params.add(map.get("optUser"));
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("status")))){
			sql.append(" and t.status = ? ");
			params.add(temp);
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("supplierName")))){
			sql.append(" and t.supplier like ? ");
			params.add("%" + temp + "%");
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("startDate")))){
			sql.append(" and date_format(t.req_date,'%Y-%m-%d') >= ?");
			params.add(temp);
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("endtDate")))){
			sql.append(" and date_format(t.req_date,'%Y-%m-%d') <= ?");
			params.add(temp);
		}
		sql.append(" order by t.buy_id desc");
		return Db.find(sql.toString(), Utils.getObjParam(params)); 
	}
	
	public Record getBuyInfo(String buyId){
		String sql = "select t.*,t1.user_name buy_user_name,t2.user_name director_name,t3.user_name manager_name from bs_buy_apply t" +
				" left join ts_user t1 on t.buy_user = t1.user_id " +
				" left join ts_user t2 on t.director = t2.user_id " +
				" left join ts_user t3 on t.manager = t3.user_id " +
				" where  t.buy_id = ?";
		return Db.findFirst(sql, buyId);
	}
	
	public List<Record> getBuyDetail(String buyId){
		String sql = "select * from bs_buy_product where buy_id = ? ";
		return Db.find(sql, buyId);
	}
	
	public String save(Map<String, Object> map){
		List<Object> params = new ArrayList<Object>();
		StringBuffer sql = new StringBuffer();
		sql.append("insert into bs_buy_apply(log_code,buy_code,buy_user,req_date,buy_use,supplier,supplier_addr," +
				"supplier_linkman,supplier_phone,arrive_date,apply_reason,customer,cust_addr,cust_linkman,cust_phone," +
				"remark,status) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,10)");
		String logCode = CreateCode.createBuyId();
		String buyCode = CreateCode.createBuyNo();
		params.add(logCode);
		params.add(buyCode);
		params.add(map.get("buyUser"));
		params.add(map.get("reqDate"));
		params.add(map.get("buyUse"));
		params.add(map.get("supplier"));
		params.add(map.get("supplierAddr"));
		params.add(map.get("supplierLinkman"));
		params.add(map.get("supplierPhone"));
		params.add(map.get("arriveDate"));
		params.add(map.get("applyReason"));
		params.add(map.get("customer"));
		params.add(map.get("custAddr"));
		params.add(map.get("custLinkman"));
		params.add(map.get("custPhone"));
		params.add(map.get("remark"));
		Db.update(sql.toString(), Utils.getObjParam(params));
		super.setCRUD("采购申请", "C", "新增采购申请：" + map.get("logCode"));
		String idxSql = "SELECT LAST_INSERT_ID() from dual ";
		String id = Db.queryColumn(idxSql).toString();
		return id;
	}
	
	public void update(Map<String, Object> map){
		List<Object> params = new ArrayList<Object>();
		String sql = " update bs_buy_apply set buy_use=?,supplier=?,supplier_addr=?," +
				"supplier_linkman=?,supplier_phone=?,arrive_date=?,apply_reason=?,customer=?,cust_addr=?," +
				"cust_linkman=?,cust_phone=?,remark=? where buy_id = ?";
//		params.add(map.get("reqDate"));
		params.add(map.get("buyUse"));
		params.add(map.get("supplier"));
		params.add(map.get("supplierAddr"));
		params.add(map.get("supplierLinkman"));
		params.add(map.get("supplierPhone"));
		params.add(map.get("arriveDate"));
		params.add(map.get("applyReason"));
		params.add(map.get("customer"));
		params.add(map.get("custAddr"));
		params.add(map.get("custLinkman"));
		params.add(map.get("custPhone"));
		params.add(map.get("remark"));
		params.add(map.get("buyId"));
		Db.update(sql, Utils.getObjParam(params));
		super.setCRUD("采购申请", "U", "修改采购申请：" + map.get("buyId"));
	}
	
	public void delete(String buyId){
		String sql = "delete from bs_buy_apply where buy_id = ? ";
		Db.update(sql, buyId);
		super.setCRUD("采购申请", "D", "删除采购申请："+ buyId);
	}
	
	public void saveDetail(String buyId, Object[][] params, int length){
		String sql = "insert into bs_buy_product(buy_id,product_name,model,num,unit_price,total_price) values (?,?,?,?,?,(?*?))";
		Db.batch(sql, params, length);
		super.setCRUD("采购申请", "C", "新增采购申请明细："+ buyId);
	}
	
	public void deleteDetail(String buyId){
		String sql = "delete from bs_buy_product where buy_id = ? ";
		Db.update(sql, buyId);
		super.setCRUD("采购申请", "D", "删除采购申请明细："+ buyId);
	}
}
